package com.backend.teamapi.controller;

import com.backend.teamapi.model.User;
import com.backend.teamapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpSession;

@RestController
@RequestMapping("/api")
public class UserController {

    @Autowired
    private UserService userService;

    // 用户注册
    @PostMapping("/register")
    public User register(@RequestBody User user) {
        return userService.register(user);
    }
    
    //用户登录
    @PostMapping("/login")
    public User login(@RequestBody User user, HttpSession session) {
        User loggedInUser = userService.login(user.getUsername(), user.getPassword());
        if (loggedInUser != null) {
            session.setAttribute("user", loggedInUser); // 将用户信息保存到 session 中
        }
        return loggedInUser;
    }

    // 用户退出登录
    @PostMapping("/logout")
    public String logout(HttpSession session) {
        session.invalidate(); // 销毁 session
        return "退出登录成功";
    }

}